/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbeans.administrator;

import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author administrator
 */
@ManagedBean(name = "loggingBean")
@RequestScoped
public class LoggingBean implements Serializable{

    private File logFile;
    private StreamedContent logFileContent;  
    
    public LoggingBean() {
        
        try {
            logFile = new File("logs/server.log");
            System.out.println(logFile.getAbsoluteFile());
            InputStream stream = new FileInputStream(logFile);
            logFileContent = new DefaultStreamedContent(stream, "text/plain", "serverlog.log");  
        } catch (FileNotFoundException ex) {
            Logger.getLogger(LoggingBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
  
    public StreamedContent getLogFileContent() {  
        InputStream stream = null;
        try {
            stream = new FileInputStream(logFile);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(LoggingBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        logFileContent = new DefaultStreamedContent(stream, "text/plain", "serverlog.log"); 
        return logFileContent;  
    }    
    
    public ArrayList<String> getLogs(){
        ArrayList<String> logList = new ArrayList<String>();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(logFile));
            String line;
            
            while((line=reader.readLine()) != null){
                logList.add(line);
            }
           
        } catch (IOException ex) {
            System.err.println(ex);
        }
        return logList;
    }

    public File getLogFile() {
        return logFile;
    }

    public void setLogFile(File logFile) {
        this.logFile = logFile;
    }
    
    
    
}
